package controllers

import (
	//	"encoding/json"
	//	"errors"
	"gitee.com/lzqysoft/ilive/models"
	//	"strconv"
	//	"strings"
	"gitee.com/lzqysoft/ilive/libs"
	"time"

	"github.com/astaxie/beego"
	//	"fmt"
	//	"gitee.com/lzqysoft/ilive/libs"

	//	"io/ioutil"
	"os"
	"path"
)

// oprations for Userprof
type UploadController struct {
	beego.Controller
}

// @Title PhotoUpload
// @Description PhotoUpload
// @Success PhotoUpload success!
// @Failure
// @router /PhotoUpload [post]
func (c *UploadController) PhotoUpload() {
	var tod Jdata
	tod.Code = 0
	tokenString := c.GetString("token")
	user, err := GetUserByToken(tokenString)
	if err != nil {
		tod.Code = 204
		tod.Message = "用户认证错误！"
		beego.Error(tod.Message, err)
		c.Data["json"] = tod
		c.ServeJSON()
		c.StopRun()
	}
	f, h, err := c.GetFile("file")
	if err != nil {
		tod.Code = 205
		tod.Message = "文件读取错误！"
		beego.Error(tod.Message, err)
		c.Data["json"] = tod
		c.ServeJSON()
		c.StopRun()
	}
	defer f.Close()
	filename := libs.GetGuid()
	filetype := path.Ext(h.Filename)
	newName := filename + filetype
	//	videopath := "data/root/imgs/" + user.Name
	videopath := "imgs/" + user.Name
	//	err = log.CheckLogPath(videopath)
	err = libs.CheckPath(videopath)
	if err != nil {
		tod.Code = 206
		tod.Message = "文件路径错误！"
		beego.Error(tod.Message, err)
		c.Data["json"] = tod
		c.ServeJSON()
		c.StopRun()
	}
	file := videopath + "/" + newName
	err = c.SaveToFile("file", file)
	if err != nil {
		tod.Code = 207
		tod.Message = "文件保存错误！"
		beego.Error(tod.Message, err)
		c.ServeJSON()
		c.StopRun()
	}
	beego.Debug(h.Filename)

	if err != nil {
		tod.Code = 207
		tod.Message = "文件保存错误！"
		beego.Error(tod.Message, err)
		c.ServeJSON()
		c.StopRun()
	}

	fid, _, err := libs.SetFile(file)
	tod.Jdata = fid
	os.Remove(file)
	c.Data["json"] = tod
	c.ServeJSON()
}

// @Title VideoUpload
// @Description VideoUpload
// @Success VideoUpload success!
// @Failure
// @router /VideoUpload [post]
func (c *UploadController) VideoUpload() {
	var tod Jdata
	tod.Code = 0
	tokenString := c.GetString("token")

	user, err := GetUserByToken(tokenString)
	if err != nil {
		tod.Code = 204
		tod.Message = "用户认证错误！"
		c.Data["json"] = tod
		c.ServeJSON()
		c.StopRun()
	}
	beego.Debug(user)
	//	rdata, err := c.AesDecode(c.GetString("tdata"))
	//	if err != nil {
	//		c.RetError(ErrToken)
	//	}
	//	sdata, _ := rdata.(map[string]interface{})

	//	org, err := models.GetOrganizationById(user.Organization.Id)
	//	if err != nil {
	//		tod.Code = 205
	//		tod.Message = "文件保存错误！"
	//		c.Data["json"] = tod
	//		c.ServeJSON()
	//		c.StopRun()
	//	}
	f, h, err := c.GetFile("file")

	if err != nil {
		tod.Code = 205
		c.Data["json"] = tod
		tod.Message = "文件读取错误！"
		c.ServeJSON()
		c.StopRun()
	}
	defer f.Close()
	filename := libs.GetGuid()
	filetype := path.Ext(h.Filename)
	newName := filename + filetype
	percent := models.GetUsedDiskPercent(*user.Organization)
	if percent > 95 {
		tod.Code = 209
		c.Data["json"] = tod
		tod.Message = "客户磁盘空间不足！"
		c.ServeJSON()
		c.StopRun()
	}
	//	videopath := "imgs/" + user.Organization.Code
	videopath := "/data/root/live/"
	err = libs.CheckPath(videopath)
	if err != nil {
		tod.Code = 205
		tod.Message = "文件读取错误！"
		c.Data["json"] = tod
		c.ServeJSON()
		c.StopRun()
	}
	file := videopath + newName

	err = c.SaveToFile("file", file)
	if err != nil {
		tod.Code = 205
		tod.Message = "文件保存错误！"
		c.Data["json"] = tod
		c.ServeJSON()
		c.StopRun()
	}
	_, duration, dur, imgPath := libs.FfmpegActImag(file)
	//	v.Url = urlpath + "/" + newName
	_, err = os.Stat(imgPath)

	if err != nil {
		tod.Code = 205
		tod.Message = "文件保存错误！"
		c.Data["json"] = tod
		c.ServeJSON()
		c.StopRun()
	}

	beego.Debug("file is %v:", file)
	beego.Debug("img is %v:", imgPath)
	ifid, _, _ := libs.SetFile(imgPath)
	fid, size, err := libs.SetFile(file)
	if err != nil {
		tod.Code = 205
		tod.Message = "文件保存错误！"
		c.Data["json"] = tod
		c.ServeJSON()
		c.StopRun()
	}
	var v models.Video
	v.Organization = user.Organization
	v.OrgName = user.Organization.Name
	v.Type = 1

	v.Title = h.Filename
	//	scope := sdata["Scope"].(string)
	//	v.Scope, _ = strconv.ParseInt(scope, 10, 64)
	//	v.Remark = sdata["Remark"].(string)

	v.Url = fid
	v.Length = duration
	v.Longtime = dur
	v.Clicks = 0
	v.Lng = 0
	v.Lat = 0
	v.Address = "-"
	//	size, _ := libs.FileSize(videopath + "/" + newName)

	v.Size = size / 1024
	v.Img = ifid
	v.Status = 1
	v.Creater = user.Name
	v.Ctime = time.Now()
	v.Updater = v.Creater
	v.Utime = v.Ctime
	//	id, err := models.AddVideo(&v)
	//	if err != nil {

	//		c.RetError(ErrDatabase)
	//	}
	beego.Debug("filenamefilename:", err, h.Filename)
	os.Remove(file)
	os.Remove(imgPath)
	tod.Jdata = v
	c.Data["json"] = tod
	c.ServeJSON()

	//	c.RetSuccess(v)

}
